package com.one.efaimaly.main.ui.binder;

import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;

import com.one.common.common.order.AddMaterialItem;
import com.one.common.common.order.MaterialItem;
import com.one.common.config.OrderState;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.utils.StringUtils;
import com.one.common.utils.list.ListUtils;
import com.one.common.view.multitytype.adapter.BaseItemBinder;
import com.one.common.view.multitytype.adapter.BaseViewHolderMulti;
import com.one.common.view.widget.FlowLayout;
import com.one.efaimaly.R;
import com.one.efaimaly.main.model.item.OrderItem;
import com.one.efaimaly.order.model.extra.AddMaterialListExtra;
import com.one.efaimaly.order.presenter.OrderStateHandle;

import java.util.ArrayList;
import java.util.List;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/8/5 下午8:29
 * description ：订单
 **/
public class OrderBinder extends BaseItemBinder<OrderItem> {
    public OrderBinder(OrderBtnClickListener orderBtnClickListener) {
        super(R.layout.item_order);
        this.orderBtnClickListener = orderBtnClickListener;
    }

    @Override
    protected void bindView(@NonNull final BaseViewHolderMulti holder, @NonNull final OrderItem item) {
        holder.setText(R.id.tv_service_des, item.getMaintail_remark());
        if(StringUtils.isNotBlank(item.getService_start_time()) && StringUtils.isNotBlank(item.getService_end_time())){
//            String time = TimeUtils.getServiceItemStarEndTime(item.getService_start_time(), item.getService_end_time());
            String time = item.getService_start_time().substring(0, 10);
            String timeArea = item.getService_start_time().substring(11, 16) + "-" + item.getService_end_time().substring(11, 16);
//            holder.setText(R.id.tv_service_time,  "服务时间 "+time + "  " + timeArea);
            holder.setText(R.id.tv_time, time + "  " + timeArea);
        }
        holder.setText(R.id.tv_location, item.getMaintailOrderAddressBean().getAddress());
        holder.getView(R.id.ll_order_item).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterManager.getInstance().go(RouterPath.ORDER_DETAIL, new DefaultExtra(item.getOrder_id()+""));
            }
        });
        FlowLayout flowLayout = holder.getView(R.id.fl_keyword);
        if (StringUtils.isNotBlank(item.getParts_names())) {
            List<String> list = ListUtils.arrayToList(item.getParts_names().split(","));
            flowLayout.setViews(list);
        }
        if(item.getIs_guarantee()){
            holder.getView(R.id.iv_bao).setVisibility(View.VISIBLE);
        } else {
            holder.getView(R.id.iv_bao).setVisibility(View.GONE);
        }
        TextView btn1 = holder.getView(R.id.tv_btn_cancel);
        TextView btn2 = holder.getView(R.id.tv_btn_navigation);
        TextView btn3 = holder.getView(R.id.tv_btn_confirm);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               orderBtnClickListener.onCancel(item);
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 弹窗选择地图 腾讯，高德，百度
                orderBtnClickListener.onNavigation(item);
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳添加材料
                if(item.getOrder_state().equals(OrderState.WAIT_VISIT)){
                    RouterManager.getInstance().go(RouterPath.ADD_MATERIAL,
                            getExtra(item));
                } else {
                    RouterManager.getInstance().go(RouterPath.ORDER_DETAIL, new DefaultExtra(item.getOrder_id()+""));
                }
            }
        });
        OrderStateHandle.setStateView(item, btn1, btn2, btn3);
    }

    private OrderBtnClickListener orderBtnClickListener;
    public interface OrderBtnClickListener{
        void onNavigation(OrderItem item);
        void onCancel(OrderItem item);
    }

    /**
     * 获取添加列表
     */
    private AddMaterialListExtra getExtra(OrderItem data) {
        List<AddMaterialItem> payData = new ArrayList<>();
        List<AddMaterialItem> freeData = new ArrayList<>();
        for (MaterialItem item : data.getMaintailOrderMaterialBeans()) {
            if (item.getPrice() > 0) {
                payData.add(new AddMaterialItem(item, false));
            } else {
                freeData.add(new AddMaterialItem(item, true));
            }
        }
        return new AddMaterialListExtra(freeData, payData, data.getOrder_id()+"");
    }
}
